package ex.hql.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import ex.hql.mapper.EmployeeMapper;
import ex.hql.mapper.MailLogMapper;
import ex.hql.pojo.*;
import ex.hql.service.IEmployeeService;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hql
 * @since 2021-08-19
 */
@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements IEmployeeService {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private MailLogMapper mailLogMapper;

    /**
     * 获取所有员工（分页）
     * @param currentPage
     * @param size
     * @param employee
     * @param beginDateScope
     * @return
     */
    @Override
    public RespPageBean getEmployeeByPage(Integer currentPage, Integer size, Employee employee, LocalDate[] beginDateScope) {
        //开启分页，第一个当前页，第二个参数每页条数
        Page<Employee> page=new Page<>(currentPage,size);
        IPage<Employee> iPage = getBaseMapper().getEmployeeByPage(page, employee, beginDateScope);
        RespPageBean respPageBean = new RespPageBean();
        respPageBean.setTotal(iPage.getTotal());
        respPageBean.setData(iPage.getRecords());
        return respPageBean;
    }

    /**
     * 获取工号
     * @return
     */
    @Override
    public RespBean maxWorkID() {
        List<Map<String, Object>> maps = getBaseMapper().selectMaps(new QueryWrapper<Employee>().select("max(workID)"));
        return RespBean.success(null,String.format("%08d",Integer.parseInt((maps.get(0).get("max(workID)").toString())+1)));
    }

    /**
     * 添加员工
     * @return
     */
    @Override
    public RespBean addEmp(Employee employee) {
        //处理合同期限，保留两位小数
        LocalDate beginContract=employee.getBegincontract();
        LocalDate endContract = employee.getEndcontract();
        long days = beginContract.until(endContract, ChronoUnit.DAYS);
        DecimalFormat decimalFormat = new DecimalFormat("##.00");
        employee.setContractterm(Double.parseDouble(decimalFormat.format(days/365.00)));
        if(getBaseMapper().insert(employee)==1){

            Employee emp=getBaseMapper().getEmployee(employee.getId()).get(0);
            //数据库记录发送的消息
            //String msgID = UUID.randomUUID().toString();
            String msgID="123456";
            MailLog mailLog=new MailLog();
            mailLog.setMsgid(msgID);
            mailLog.setEid(emp.getId());
            mailLog.setStatus(0);
            mailLog.setRoutekey(MailConstants.MAIL_ROUTING_KEY_NAME);
            mailLog.setCount(0);
            mailLog.setExchange(MailConstants.MAIL_EXCHANGE_NAME);
            mailLog.setTrytime(LocalDateTime.now().plusMinutes(MailConstants.MSG_TIMEOUT));
            mailLog.setCreatetime(LocalDateTime.now());
            mailLog.setUpdatetime(LocalDateTime.now());
            mailLogMapper.insert(mailLog);
            //发送消息
            rabbitTemplate.convertAndSend(MailConstants.MAIL_EXCHANGE_NAME,MailConstants.MAIL_ROUTING_KEY_NAME,emp,new CorrelationData(msgID));
            return RespBean.success("添加员工成功！");
        }
        return RespBean.error("添加员工失败！");
    }


    /**
     * 查询员工
     * @param id
     * @return
     */
    @Override
    public List<Employee> getEmployee(Integer id) {
        return getBaseMapper().getEmployee(id);
    }


    /**
     * 获取所有员工工资账套
     * @param currentPage
     * @param size
     * @return
     */
    @Override
    public RespPageBean getEmployeeWithSalary(Integer currentPage, Integer size) {
        Page<Employee> page=new Page<>(currentPage,size);
        IPage<Employee> iPage=getBaseMapper().getEmployeeWithSalary(page);
       return new RespPageBean(iPage.getTotal(),iPage.getRecords());
    }
}
